package com.ruoyix.weblog.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyix.weblog.domain.vo.BlogCommentResVo;
import jakarta.validation.constraints.*;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.core.validate.QueryGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.weblog.domain.vo.BlogCommentVo;
import com.ruoyix.weblog.domain.bo.BlogCommentBo;
import com.ruoyix.weblog.service.IBlogCommentService;
import com.ruoyix.common.core.page.TableDataInfo;

/**
 * 文章评论
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/comment")
public class BlogCommentController extends BaseController {

    private final IBlogCommentService iBlogCommentService;

    /**
     * 查询文章评论列表
     */
    @SaCheckPermission("weblog:comment:list")
    @GetMapping("/list")
    public R<List<BlogCommentResVo>> list(BlogCommentBo bo, PageQuery pageQuery) {
        return R.ok(iBlogCommentService.getNestedComments(bo));
    }

    /**
     * 导出文章评论列表
     */
    @SaCheckPermission("weblog:comment:export")
    @Log(title = "文章评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BlogCommentBo bo, HttpServletResponse response) {
        List<BlogCommentVo> list = iBlogCommentService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章评论", BlogCommentVo.class, response);
    }

    /**
     * 获取文章评论详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("weblog:comment:query")
    @GetMapping("/{id}")
    public R<BlogCommentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBlogCommentService.queryById(id));
    }

    /**
     * 新增文章评论
     */
    @SaCheckPermission("weblog:comment:add")
    @Log(title = "文章评论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogCommentBo bo) {
        return toAjax(iBlogCommentService.insertByBo(bo));
    }

    /**
     * 修改文章评论
     */
    @SaCheckPermission("weblog:comment:edit")
    @Log(title = "文章评论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogCommentBo bo) {
        return toAjax(iBlogCommentService.updateByBo(bo));
    }

    /**
     * 删除文章评论
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:comment:remove")
    @Log(title = "文章评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBlogCommentService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
